package com.zwx.tree.leetcode_4;

/**
 * 对称二叉树
 * <p>
 * 给定一个二叉树，检查它是否是镜像对称的
 *
 * @author 文希
 * @create 2019-07-07 22:43
 */
public class IsSymmetric {

    /**
     * 递归
     * 我们需要递归判断left.left==right.right&&left.right==right.left即可
     *
     * @param root
     * @return
     */
    private static boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return symmetric(root.left, root.right);
    }

    private static boolean symmetric(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null) {
            return false;
        }
        return (left.val == right.val)
                && symmetric(left.left, right.right)
                && symmetric(left.right, right.left);

    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
